package com.triones.biz.infra.controller.api.file;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.io.IoUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.triones.biz.infra.controller.enums.SceneEnum;
import com.triones.biz.infra.entity.file.vo.FileUploadVO;
import com.triones.biz.infra.entity.file.vo.FileVO;
import com.triones.biz.infra.service.file.FileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * API - 文件上传api
 *
 * @author triones
 * 2023-09-25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/uploader")
@Validated
@Slf4j
public class ApiUploaderController {

    private final FileService fileService;

    /**
     * 上传文件
     * @param scene {@link SceneEnum#getScene()} （"意见反馈"："feedback"）
     */
    @PostMapping("/upload")
    @SaIgnore
    public R<FileVO> uploadFile(FileUploadVO uploadVO, String scene) throws Exception {
        MultipartFile file = uploadVO.getFile();
        String path = uploadVO.getPath();
        FileVO fileVo = fileService.upload(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()),scene);
        return R.ok(fileVo);
    }
}
